<?php
require 'cabesario.php';
include 'model/Contact.php';
if (isset($_SESSION["autenticacao"])) {
    if ($_SESSION['autenticacao'] == "ok") {
        if ($_POST != null) {
            $file = $_FILES['uploadedfile']['tmp_name'];
            $doc = fopen($file, "r");
            $id = 0;
            unset($_SESSION["session_contacts"]);
            while (!feof($doc)) {
                $str = fgetss($doc, 4096);
                if ($id > 0 && strlen($str) > 0) {
                    $informations = explode(',', $str);
                    $description = $informations[0] . ' ' . $informations[1];
                    $email = $informations[28];
                    $fone = "";
                    $_SESSION['session_contacts'][] = array('id' => $id, 'name' => $description, 'email' => $email, 'fone' => $fone);
                }
                $id++;
            }
            fclose($doc);
        }
        ?>
        <form enctype="multipart/form-data" action="import.php" method="POST" style="color:black;">
            <input type="hidden" name="url" value="xml_gmail.php" />
            <input name="uploadedfile" type="file" accept=".csv" required="true" />
            <br/><hr/><input class="style_button" type="submit" value="Carregar">
        </form>
        <form method="post" action="importcontact.php">
            <input type="hidden" name="action" />
            <?php
            if (isset($_SESSION["session_contacts"])) {

                foreach ($_SESSION["session_contacts"] as $a) {
                    ?>

                    <div id="contact">
                        <div style="float: left;margin-top: 10px; padding-right: 15px;">
                            <input type="checkbox" name="id_contact[]" value="<?php echo $a['id'] ?>" />
                        </div>
                        <div style="float: left;">
                            <div class="name"><?php echo $a['name'] ?></div>
                            <div class="description"><?php echo $a['email'] . ' - ' . $a['fone']; ?></div>
                        </div>
                    </div>
                <?php }
                ?>
            <hr/>
                <input class="style_button" type="button" value="Todos" onclick="selectAll();"/>
                <input class="style_button" type="reset"  value="Limpar"/>
                <input class="style_button" type="submit" value="Salvar" onclick="return defineAction('salvar');"/>
                <input class="style_button" type="submit" value="Editar" onclick="return defineAction('editar');"/>
                <input class="style_button" type="submit" value="Excluir" onclick="return excluir();"/>
            </form
            <?php
        }
    }
} else {
    header("location: index.php");
}require 'rodape.php';
?>
<script lang="javascript">
    function selectAll() {
        var elements = document.getElementsByName('id_contact[]');
        for (i = 0; i < elements.length; i++) {
            elements[i].checked = true;
        }
    }
    function defineAction(action) {
        document.getElementsByName('action')[0].value = action;
        var check =  false;
        var elements = document.getElementsByName('id_contact[]');
        for (i = 0; i < elements.length; i++) {
            if(elements[i].checked){
                check = true;
            }
        }
        if(!check){
            alert('Selecione pelo menos um contato')
        }
        return check;
    }
    function excluir() {
        var action = confirm('Deseja realmente excluir?');
        if (action) {
            return defineAction('excluir');
        }
        return action;
    }
</script>